package 力扣日常刷题.木23四月.第3天403;

import java.util.*;

public class Encrypter {


    Map<Character,String> emap = new HashMap<>();
    Map<String, List<Character>> dmap = new HashMap<>();
    Set<String> strings = new HashSet<>();
    public Encrypter(char[] keys, String[] values, String[] dictionary) {
        for (int i = 0; i < keys.length; i++) {
            emap.put(keys[i],values[i]);
            if(dmap.containsKey(values[i])) {
                List<Character> characters = dmap.get(values[i]);
                characters.add(keys[i]);
                dmap.put(values[i],characters);
            }else{
                List<Character> characters = new ArrayList<>();
                characters.add(keys[i]);
                dmap.put(values[i],characters);
            }
        }
        for (String s : dictionary) {
            strings.add(s);
        }
    }

    public String encrypt(String word1) {
        StringBuilder sb = new StringBuilder();

        for (char c : word1.toCharArray()) {
            sb.append(emap.get(c));
        }
        return sb.toString();
    }

    public int decrypt(String word2) {
        int ans = 0;
        HashSet<StringBuilder> set = new HashSet<>();
        for (int i = 0; i < word2.length(); i+=2) {
            List<Character> characters = dmap.get(word2.substring(i, i + 2));
            HashSet<StringBuilder> temp = new HashSet<>();
            for (Character character : characters) {
                if(set.isEmpty()){
                    set.add(new StringBuilder().append(character));
                    temp.add(new StringBuilder().append(character));
                }else{
                    for (StringBuilder stringBuilder : set) {
                        StringBuilder tt = new StringBuilder(stringBuilder).append(character);
                        temp.add(tt);
                    }
                }
            }
            set = temp;
        }


        for (StringBuilder stringBuilder : set) {
            if(strings.contains(stringBuilder.toString()))ans++;
        }
        return ans;
    }


}
